package com.example.b_308.medical.view.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.b_308.medical.R;
<<<<<<< HEAD
import com.example.b_308.medical.view.SysApplication;
=======
import com.example.b_308.medical.SysApplication;
>>>>>>> origin/master
import com.example.b_308.medical.comment.view.TitleBar;

import java.io.Serializable;

/**
 * Created by B-308 on 2017/11/15.
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    AlertDialog alertDialog;
    AlertDialog.Builder builder;
    LinearLayout ll;
    TitleBar titleBar;

    //=============沉侵式==(begin)=================
    private static View mStatusBarView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        SysApplication.getInstance().addActivity(this);
        ll = (LinearLayout) findViewById(R.id.ll);
        titleBar = (TitleBar) findViewById(R.id.titlebar);
        builder = new AlertDialog.Builder(this);
        //设置状态栏为透明
        // sdk 4.4
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            if (mStatusBarView != null) {
                ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
                root.removeView(mStatusBarView);
            }
            return;
        }

        // sdk 5.x
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.setStatusBarColor(Color.TRANSPARENT);
            return;
        }
        if (null != getSupportActionBar())
            getSupportActionBar().hide();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        ll.addView(LayoutInflater.from(this).inflate(layoutResID, null));
    }

    public TitleBar getTitleBar(){return titleBar;}

    /**
     * 设置控件的paddingTop, 使它不被StatusBar覆盖
     */
    private static void setStatusBarPadding(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // marginTop： 状态栏的高度
            int marginTop = getStatusBarHeight(view.getContext());
            view.setPadding(view.getPaddingLeft(), marginTop,
                    view.getPaddingRight(), view.getPaddingBottom());
            return;
        }
    }


    public void tent2(Class cla) {
        startActivity(new Intent(this, cla));
    }

    public  void tent2(Class cla,String key,int type) {
        Intent intent=new Intent(this, cla);
        intent.putExtra(key,type);
        startActivity(intent);
    }

    public <T> void tentForResoult(Class cla, int requestCode) {
        Intent intent = new Intent(this, cla);
        startActivityForResult(intent, requestCode);
    }

    public <T> void tent2(Class cla, T t, String key) {
        Intent intent = new Intent(this, cla);
        if (t instanceof String) {
            intent.putExtra(key, (String) t);
        }
        if (t instanceof String[]) {
            intent.putExtra(key, (String[]) t);
        }
        if (t instanceof Long) {
            intent.putExtra(key, (Long) t);
        }
        if (t instanceof Double) {
            intent.putExtra(key, (Double) t);
        }
        if (t instanceof Integer) {
            intent.putExtra(key, (Integer) t);
        }
        if (t instanceof Serializable) {
            intent.putExtra(key, (Serializable) t);
        }
    }


    @Override
    public void fail(String message) {
        builder.setTitle("提示").setMessage(message).setPositiveButton("确定", null);
        alertDialog = builder.create();
        alertDialog.show();
        ;
    }

    public <T extends View> T findView(int resouceId) {
        return (T) findViewById(resouceId);
    }

    private static int getStatusBarHeight(Context context) {
        return 0;
    }

}
